package com.sakura.二分答案;

public class Code2226_每个小孩最多能分到多少糖果 {

    public int maximumCandies(int[] candies, long k) {
        int left = 0;
        int right = 0;
        for (int candy : candies) {
            right = Math.max(right, candy);
        }

        while (left + 1 < right) {
            int mid = left + (right - left) / 2;
            if (check(candies, mid, k)) {
                left = mid;
            } else {
                right = mid;
            }
        }
        return left;
    }
    public boolean check(int[] candies, int mid, long k) {
        long sum = 0;
        for (int candy : candies) {
            sum += candy / mid;
        }
        return sum >= k;
    }
}
